﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class Templates_Download_Default : BaseTemplatePage
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            Acms.Bll.PostControl control = AcmsContext.CurrentPost.FindPostControl("Download");
            if (control != null)
            {
                Acms.AcmsControls.DownloadableFileContentControlProperties prop = control.GetProperties<Acms.AcmsControls.DownloadableFileContentControlProperties>();
                if (prop != null && prop.HasResource())
                {
                    //log visit
                    ClimateChangeChampionsData.Download tmp = new ClimateChangeChampionsData.Download();
                    tmp.Created = DateTime.Now;
                    tmp.Name = AcmsContext.CurrentPost.DisplayName;
                    tmp.Path = AcmsContext.CurrentPost.Path;
                    if (Request.IsAuthenticated)
                        tmp.UserId = User.Identity.User.Id;
                    ClimateChangeChampionsData.DataContext context = new ClimateChangeChampionsData.DataContext();
                    context.Downloads.InsertOnSubmit(tmp);
                    context.SubmitChanges();
                    Response.ClearContent();
                    Response.ClearHeaders();
                    Response.Clear();
                    Server.Transfer(string.Concat("~/File.aspx?Id=", prop.ResourceId));
                }
                throw new ApplicationException("File to download does not exist");
            }
            else
            {
                throw new ApplicationException("File to download does not exist");
            }
        }

    }
}
